using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;

namespace ShapeEditor
{
    public partial class BoundaryControl : UserControl
    {
        private BoxBoundaryProxy boxBoundaryProxy;
        private AssetManager assetMan;
        private delegate void BoundaryHandler(BoxBoundaryProxy bbp);

        public BoundaryControl()
        {
            
            InitializeComponent();
            assetMan = AssetManager.Instance;
            assetManagerDropDownControl1.Init(typeof(IShape));
            assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
        }
        #region Public Properties
        //
        // Public properties
        //

        public BoxBoundaryProxy BoxBoundary
        {
            get { return boxBoundaryProxy; }
        }
        //
        // Set the focus of this control to a new BoxBoundary.  Be careful of thread issues
        private void SetBoxBoundary_thisThread(BoxBoundaryProxy BoxBoundary)
        {
            boxBoundaryProxy = null;
            //
            // Toggle Enabled based on whether the SlicePlane is null or not
            //
            if (BoxBoundary == null)
            {
                this.Enabled = false;
            }
            else
            {
                this.Enabled = true;
                SharedBox box = BoxBoundary.Box as SharedBox;
                assetManagerDropDownControl1.SelectItem(box);
                // Set radio boxes
                if (BoxBoundary.DrawFrontFaces && BoxBoundary.DrawBackFaces)
                {
                    radioButton3.Checked = true;
                }
                else if (BoxBoundary.DrawBackFaces)
                {
                    radioButton1.Checked = true;   
                }
                else if (BoxBoundary.DrawFrontFaces)
                {
                    radioButton2.Checked = true;
                }
            }
            boxBoundaryProxy = BoxBoundary;
        }
        //
        // Force the event to get called on the proper thread.  The DataGridView complains if you try
        //  to use it on the wrong thread.
        //
        public void SetBoxBoundary(BoxBoundaryProxy BoxBoundary)
        {
            BoundaryHandler newEvent = new BoundaryHandler(SetBoxBoundary_thisThread);
            object[] args = new object[1];
            args[0] = BoxBoundary;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(newEvent, args);
                else
                    newEvent(BoxBoundary);
            }
            catch { }
        }
        #endregion

        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged asset)
        {
            if (boxBoundaryProxy != null)
            {
                // Assume for now only boxes
                boxBoundaryProxy.Box = (SharedBox)asset;
            }
         }

        public IShape SelectShapeDefault()
        {
            List<IAssetManaged> shapes = assetMan.FindAll(typeof(SharedBox));

            if (shapes.Count > 0)
            {
                assetManagerDropDownControl1.SelectItem(shapes[0]);
                return shapes[0] as IShape;
            }

            return null;
        }

        public delegate void BoxBoundaryVisibility(bool isVisible);

        public void Visibility(bool isVisible)
        {
            BoxBoundaryVisibility boxBoundaryVisibility = new BoxBoundaryVisibility(IsVisible_ThisThread);

            object[] args = new object[1];
            args[0] = isVisible;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(boxBoundaryVisibility, args);
                else
                    boxBoundaryVisibility(isVisible);
            }
            catch { }

        }

        public void IsVisible_ThisThread(bool isVisible)
        {
            this.Visible = isVisible;
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (boxBoundaryProxy != null && radioButton1.Checked)
            {
                boxBoundaryProxy.DrawBackFaces = true;
                boxBoundaryProxy.DrawFrontFaces = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (boxBoundaryProxy != null && radioButton2.Checked)
            {
                boxBoundaryProxy.DrawFrontFaces = true;
                boxBoundaryProxy.DrawBackFaces = false;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (boxBoundaryProxy != null && radioButton3.Checked)
            {
                boxBoundaryProxy.DrawBackFaces = true;
                boxBoundaryProxy.DrawFrontFaces = true;
            }
             
        }
    }
}
